package com.eazy.lkv2.web.controller.system.listener;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.eazy.lkv2.core.db.ZQ;
import com.eazy.lkv2.core.web.utils.SystemMail;
import com.eazy.lkv2.web.controller.BaseController;
import com.eazy.lkv2.web.entity.Mail;
import com.eazy.lkv2.web.service.system.listener.SystemEmailService;

/**
 * 系统邮件管理
 * @author apple
 *
 */
@Controller
@RequestMapping("/email")
public class SystemEmailController extends BaseController<Mail,Integer> {

	@Autowired
	private SystemEmailService systemEmailService;
	
	/**
	 * 邮件人员列表
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView list() {
		List<Mail> list = systemEmailService.queryByObject(new Mail("1"));
		return getSuccess("sys/email_list","data",list);
	}
	
	/**
	 * 发送邮件 
	 */
	@RequestMapping(value = "/send", method = { RequestMethod.POST})
	public void send(@RequestParam String title,@RequestParam String content,@RequestParam String toAddress) {
		String type = getPara("type");
		String path = getPara("path");
		if(type.equals("0")) {
			SystemMail.getInstance().sendText(title, content, toAddress);
		} else if(type.equals("1")) {
			SystemMail.getInstance().sendHTML(title, content, toAddress, path);
		}
		// 2016-7-18 实现记录发送人功能
		ZQ.commonInsert("insert into email_log(email_log.from,email_log.to,message) values(?,?,?)",new Object[]{"testlksy@163.com",toAddress,content});
		renderResult(true);
	}
	
	/**
	 * 添加邮件人员
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.POST, RequestMethod.GET })
	public void add(Mail mail) {
		mail.setStatus("1"); // 正常启用
		renderResult(create(mail) > 1);
	}
	
	/**
	 * 删除人员
	 */
	@RequestMapping(value = "/delete", method = { RequestMethod.POST, RequestMethod.GET })
	public String delete() {
		systemEmailService.removeById(getPara("id"));
		return "redirect:/email/list";
	}
	
	/**
	 * 跳转到人员添加页面
	 */
	@RequestMapping(value = "/toAdd", method = { RequestMethod.POST, RequestMethod.GET })
	public String toAdd() {
		return "sys/email_add";
	}
	
	/**
	 * 跳转到发送邮件页面
	 */
	@RequestMapping(value = "/toSend/{id}", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView toSend(@PathVariable String id) {
		Mail mail = systemEmailService.findById(id);
		return getSuccess("sys/email_send",mail);
	}
}
